/** 
 * 2011-10-11 上午10:48:42 
 * UdpMessageEncoder.java 
 * author:sunb 
 */
package com.u6.udp.netty;

import java.nio.ByteBuffer;

/**
 * @author sunbin
 * 
 */
public class UdpMessageEncoder {

	public static byte[] encode(UdpMessage message) {
		ByteBuffer buffer = ByteBuffer.allocate(message.getPacketSize());
		encode(buffer, message);
		byte[] bytes = buffer.array();
		buffer.clear();
		return bytes;
	}

	public static void encode(ByteBuffer buffer, UdpMessage message) {
		buffer.putShort(message.getMessageID());
		buffer.putShort(message.getType());
		buffer.putLong(message.getSendUid());
		buffer.putLong(message.getReceiveUid());
		buffer.putInt(message.getLength());

		if (message.getLength() > 0) {
			buffer.put(message.getData());
		}
	}

}
